

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.ParallelBehaviour;
import jade.core.behaviours.SequentialBehaviour;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.proto.AchieveREResponder;
import jade.proto.states.MsgReceiver;

public class MinhProfilerAgent extends Agent {
	private AID auctioneer;
	private int waitRounds;
	private int waited;

	// setup
	public void setup() {
		System.err.println("My name is " + getLocalName()
				+ ", started correctly.");

		Object[] args = getArguments();
		if (args != null && args.length > 0) {
			waitRounds = Integer.parseInt((String) args[0]);
		} else {
			waitRounds = (int) (Math.random() * 10);
		}
		waited = 0;
		auctioneer = new AID("Seller", AID.ISLOCALNAME);

		addBehaviour(new BiddingSubscriber());
	}

	// Subscribe to the auction (step 1)
	private class BiddingSubscriber extends OneShotBehaviour {

		public void action() {
			System.err.println("BiddingSubscriber called. (Step 1)");
			ACLMessage msg = new ACLMessage(ACLMessage.SUBSCRIBE);
			msg.setSender(getAID());
			msg.addReceiver(auctioneer);
			send(msg);

			SequentialBehaviour seq = new SequentialBehaviour();
			seq.addSubBehaviour(new BiddingSubscriptionReceiver());
			seq.addSubBehaviour(new BiddingBehaviour());
			addBehaviour(seq);
		}
	}

	// Receives subscriptionconfirmation (step 3)
	private class BiddingSubscriptionReceiver extends OneShotBehaviour {

		public void action() {

			System.err.println("BiddingSubscriptionReceiver called. (Step 3)");

			// waits for reply
		//	ACLMessage recmsg = blockingReceive((MessageTemplate
			//		.MatchPerformative(ACLMessage.CONFIRM)));
			
			System.err.println("BiddingSubscriptionReceiver called. (Step 3.2)");

		}
	}

	// Receives a CFP and bids on the item if the price is ok! (step 5)
	private class BiddingBehaviour extends CyclicBehaviour {

		public void action() {
			MessageTemplate mt = MessageTemplate.or(MessageTemplate.or(
					MessageTemplate
							.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL),
					MessageTemplate.MatchPerformative(ACLMessage.INFORM)),
					MessageTemplate.MatchPerformative(ACLMessage.CFP));
			System.err.println("(step 5)");
			ACLMessage recmsg = blockingReceive(mt);
			ACLMessage repmsg = recmsg.createReply();
			switch (recmsg.getPerformative()) {

			case ACLMessage.INFORM:
				System.err.println("From auctioneer: " + recmsg.getContent());
				break;

			case ACLMessage.CFP:

				// tactic is to wait x rounds before bidding
				System.err.println("I'll wait for : " + (waitRounds - waited)
						+ " more rounds.");
				if (waited == waitRounds) {
					System.err.println("Time to bid!");
					repmsg.setPerformative(ACLMessage.PROPOSE);
					send(repmsg);
				}
				// wait another round
				else {
					waited++;
				}
				break;

			case ACLMessage.ACCEPT_PROPOSAL:

				System.err.println("I WON!");
				/*
				 * try {
				 * 
				 * Artifact artifact = (Artifact) recmsg.getContentObject();
				 * System.err.println("Artifact : " + artifact.getName() +
				 * " won."); waited = 0; } catch (UnreadableException e) {
				 * e.printStackTrace(); }
				 */
				waited = 0;
				break;
			default:
				break;
			}
		}
	}
}
